import {
	createRouter,
	createWebHashHistory
} from 'vue-router'

import layout from '@/layout/index'
import ArticleCreaterRouter from './modules/ArticleCreate'
import ArticleRouter from './modules/Article'
import PermissionListRouter from './modules/PermissionList'
import RoleListRouter from './modules/RoleList'
import UserManageRouter from './modules/UserManage'
import store from '@/store'


// 公开路由表
export const publicRoutes = [{
	path: '/',
	component: layout,
	redirect: '/profile',
	children: [
		// 个人中心
		{
			path: '/profile',
			name: 'profile',
			component: () => import('@/views/profile/index'),
			meta: {
				title: 'profile',
				icon: 'el-icon',
				iconName: 'User'
			}
		},
		{
			path: '/404',
			name: '404',
			component: () => import('@/views/error-page/404')
		},
		{
			path: '/401',
			name: '401',
			component: () => import('@/views/error-page/401')
		}
	]
}, {
	path: '/login',
	component: () => import('@/views/login')
}]

// 私有路由表
export const privateRoutes = [
	RoleListRouter,
	UserManageRouter,
	PermissionListRouter,
	ArticleCreaterRouter,
	ArticleRouter
]

const router = createRouter({
	history: createWebHashHistory(),
	routes: publicRoutes
})

export function resetRouter() {
	if (store.getters.userInfo && store.getters.userInfo.permission && store.getters.userInfo.permission.menus) {
		const menus = store.getters.userInfo.permission.menus

		menus.forEach(menu => {
			router.removeRoute(menu)
		})
	}
}

export default router
